#!/bin/bash

sandboxdir=$(dirname $0)
. $sandboxdir/sb_vars.sh
. $sandboxdir/sb_common.sh

# This will remove sandboxes created with a previous setup
if [ -x $TUNGSTEN_SB/sb_erase_sandbox ]
then
    $TUNGSTEN_SB/sb_erase_sandbox
fi

# This will remove sandboxes created with the current setup
$sandboxdir/sb_reset

function check_error_logs
{
    for DIR in $( ls -d $TUNGSTEN_SB/${SB_PREFIX}*/ )
    do
        LOG=$DIR/tungsten/tungsten-replicator/log/trepsvc.log
        if [ -f $LOG ]
        then
            HAS_ERROR=$(grep ERROR $LOG)
            if [ -n "$HAS_ERROR" ]
            then
                echo $DOTS_LINE
                echo "# ERROR found in $DIR:"
                echo $DOTS_LINE
                perl -ne 'BEGIN{$hold=1} $hold=0 if /ERROR/;  print unless $hold'  $LOG
                echo $DOTS_LINE
            fi
        fi
    done
}

total_pass=0
total_fail=0
total_tests=0
echo $DOTS_LINE > all_operations.log
echo "# START : $(date) " >> all_operations.log
echo $DOTS_LINE >> all_operations.log

# all_topologies='master_slave fanin all_masters star direct'
# all_topologies='star master_slave fanin all_masters direct mongodb fileapplier'

if [ -z "$all_topologies" ]
then
    all_topologies='master_slave fanin all_masters direct star'
    if [ -d $MONGODB_EXPANDED_TARBALLS/$MONGODB_VERSION ]
    then
        all_topologies="$all_topologies mongodb"
    fi
    if [ -f ./tungsten-replicator/samples/conf/datasources/file.tpl ]
    then
        all_topologies="$all_topologies fileapplier"
    fi
fi

if [ -n "$UNPRIVILEGED_USERS" ]
then
    all_topologies="master_slave"
fi

for topology in $all_topologies
do
    if [ -n "$USE_INI_FILES" ]
    then
        USE_INI_FILES=--use-ini-files
    fi
    echo $DOTS_LINE
    echo "# Testing topology $topology"
    # echo "#SSL: <$USE_SSL>"
    echo $DOTS_LINE
    echo $DOTS_LINE >> all_operations.log
    echo "# Testing topology $topology" >> all_operations.log
    echo $DOTS_LINE >> all_operations.log
    export TEST_ALL_TOPOLOGIES=1
    $sandboxdir/tungsten-sandbox $USE_INI_FILES --topology=$topology --verbose -m $MYSQL_VERSION > installation.log 2>&1
    # $sandboxdir/sb_$topology > installation.log 2>&1
    exit_code=$?
    unset TEST_ALL_TOPOLOGIES
    if [ -n "$TEST_INSTALLATION_FAILURE" -a "$exit_code" == "0" ]
    then
        echo "Testing installation failure"
        echo "Installation succeeded, but we are simulating a failure"
        exit_code=111
    fi
    if [ "$exit_code" != "0" ]
    then
        UNEXPLAINED=$(grep 'At least one replication service has experienced an error' installation.log)
        if [ -n "$UNEXPLAINED" ]
        then
            check_error_logs
        fi
        cat installation.log
        exit $exit_code
    fi
    rm -f testing.log
    $TUNGSTEN_SB/sb_test_sandbox >> testing.log 2>&1
    exit_code=$?
    pass=$(grep '^ok' testing.log | wc -l)
    fail=$(grep '^not ok' testing.log | wc -l)
    fail=$(($fail+0))
    pass=$(($pass+0))
    tests=$(($pass+$fail))
    total_pass=$(($total_pass+$pass))
    total_fail=$(($total_fail+$fail))
    total_tests=$(($total_tests+$tests))
    if [ "$exit_code" != "0" ]
    then
        cat testing.log
        echo "exit code: <$exit_code>"
        exit $exit_code
    fi
    # echo "pass:<$pass>"
    # echo "fail:<$fail>"
    cat testing.log
    if [ "$fail" != "0" ]
    then
        echo "<$fail> failures detected"
        exit 1
    fi
    echo "# Cleaning up ..."
    $TUNGSTEN_SB/sb_erase_sandbox > uninstall.log 2>&1
    exit_code=$?
    if [ "$exit_code" != "0" ]
    then
        cat uninstall.log
        exit $exit_code
    fi
    cat testing.log installation.log uninstall.log >> all_operations.log
    rm -f testing.log installation.log uninstall.log
done

echo $DOTS_LINE
echo "# Grand total"
echo "# total tests: $total_tests"
echo "# passed     : $total_pass"
echo "# failed     : $total_fail"
echo $DOTS_LINE
echo $DOTS_LINE >> all_operations.log
echo "# Grand total" >> all_operations.log
echo "# total tests: $total_tests" >> all_operations.log
echo "# passed     : $total_pass" >> all_operations.log
echo "# failed     : $total_fail" >> all_operations.log
echo $DOTS_LINE >> all_operations.log
echo "# END   : $(date) " >> all_operations.log
echo $DOTS_LINE >> all_operations.log

